home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / awksrc.zip / MISSING / STRFTIME.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  14KB  |  567 lines

  1. /*
  2.  * strftime.c
  3.  *
  4.  * Public-domain relatively quick-and-dirty implemenation of
  5.  * ANSI library routine for System V Unix systems.
  6.  *
  7.  * It's written in old-style C for maximal portability.
  8.  * However, since I'm used to prototypes, I've included them too.
  9.  *
  10.  * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
  11.  * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
  12.  * For complete POSIX semantics, add POSIX_SEMANTICS.
  13.  *
  14.  * The code for %c, %x, and %X is my best guess as to what's "appropriate".
  15.  * This version ignores LOCALE information.
  16.  * It also doesn't worry about multi-byte characters.
  17.  * So there.
  18.  *
  19.  * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
  20.  * code are included if GAWK is defined.
  21.  *
  22.  * Arnold Robbins
  23.  * January, February, March, 1991
  24.  * Updated March, April 1992
  25.  *
  26.  * Fixes from ado@elsie.nci.nih.gov
  27.  * February 1991, May 1992
  28.  */
  29.  
  30. #ifndef GAWK
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <sys/types.h>
  36. #endif
  37.  
  38. /* defaults: season to taste */
  39. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  40. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  41. #define VMS_EXT        1    /* include %v for VMS date format */
  42. #ifndef GAWK
  43. #define POSIX_SEMANTICS    1    /* call tzset() if TZ changes */
  44. #endif
  45.  
  46. #if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
  47. #define SYSV_EXT    1
  48. #endif
  49.  
  50. #if defined(POSIX2_DATE)
  51. #define adddecl(stuff)    stuff
  52. #else
  53. #define adddecl(stuff)
  54. #endif
  55.  
  56. #ifndef __STDC__
  57. #define const    /**/
  58. extern void *malloc();
  59. extern void *realloc();
  60. extern void tzset();
  61. extern char *strchr();
  62. extern char *getenv();
  63. static int weeknumber();
  64. adddecl(static int iso8601wknum();)
  65. #else
  66. extern void *malloc(unsigned count);
  67. extern void *realloc(void *ptr, unsigned count);
  68. extern void tzset(void);
  69. extern char *strchr(const char *str, int ch);
  70. extern char *getenv(const char *v);
  71. static int weeknumber(const struct tm *timeptr, int firstweekday);
  72. adddecl(static int iso8601wknum(const struct tm *timeptr);)
  73. #endif
  74.  
  75. #ifdef __GNUC__
  76. #define inline    __inline__
  77. #else
  78. #define inline    /**/
  79. #endif
  80.  
  81. #define range(low, item, hi)    max(low, min(item, hi))
  82.  
  83. #if !defined(MSDOS) && !defined(TZNAME_MISSING)
  84. extern char *tzname[2];
  85. extern int daylight;
  86. #endif
  87.  
  88. /* min --- return minimum of two numbers */
  89.  
  90. #ifndef __STDC__
  91. static inline int
  92. min(a, b)
  93. int a, b;
  94. #else
  95. static inline int
  96. min(int a, int b)
  97. #endif
  98. {
  99.     return (a < b ? a : b);
  100. }
  101.  
  102. /* max --- return maximum of two numbers */
  103.  
  104. #ifndef __STDC__
  105. static inline int
  106. max(a, b)
  107. int a, b;
  108. #else
  109. static inline int
  110. max(int a, int b)
  111. #endif
  112. {
  113.     return (a > b ? a : b);
  114. }
  115.  
  116. /* strftime --- produce formatted time */
  117.  
  118. #ifndef __STDC__
  119. size_t
  120. strftime(s, maxsize, format, timeptr)
  121. char *s;
  122. size_t maxsize;
  123. const char *format;
  124. const struct tm *timeptr;
  125. #else
  126. size_t
  127. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  128. #endif
  129. {
  130.     char *endp = s + maxsize;
  131.     char *start = s;
  132.     char tbuf[100];
  133.     int i;
  134.     static short first = 1;
  135. #ifdef POSIX_SEMANTICS
  136.     static char *savetz = NULL;
  137.     static int savetzlen = 0;
  138.     char *tz;
  139.     int tzlen;
  140. #endif
  141.  
  142.     /* various tables, useful in North America */
  143.     static char *days_a[] = {
  144.         "Sun", "Mon", "Tue", "Wed",
  145.         "Thu", "Fri", "Sat",
  146.     };
  147.     static char *days_l[] = {
  148.         "Sunday", "Monday", "Tuesday", "Wednesday",
  149.         "Thursday", "Friday", "Saturday",
  150.     };
  151.     static char *months_a[] = {
  152.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  153.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  154.     };
  155.     static char *months_l[] = {
  156.         "January", "February", "March", "April",
  157.         "May", "June", "July", "August", "September",
  158.         "October", "November", "December",
  159.     };
  160.     static char *ampm[] = { "AM", "PM", };
  161.  
  162.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  163.         return 0;
  164.  
  165.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  166.         return 0;
  167.  
  168. #ifndef POSIX_SEMANTICS
  169.     if (first) {
  170.         tzset();
  171.         first = 0;
  172.     }
  173. #else    /* POSIX_SEMANTICS */
  174.     tz = getenv("TZ");
  175.     tzlen = strlen(tz);
  176.     if (first) {
  177.         if (tz != NULL) {
  178.             savetz = (char *) malloc(tzlen + 1);
  179.             if (savetz != NULL) {
  180.                 savetzlen = tzlen + 1;
  181.                 strcpy(savetz, tz);
  182.             }
  183.         }
  184.         tzset();
  185.         first = 0;
  186.     }
  187.     /* if we have a saved TZ, and it is different, recapture and reset */
  188.     if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
  189.         i = strlen(tz) + 1;
  190.         if (i > savetzlen) {
  191.             savetz = (char *) realloc(savetz, i);
  192.             if (savetz) {
  193.                 savetzlen = i;
  194.                 strcpy(savetz, tz);
  195.             }
  196.         } else
  197.             strcpy(savetz, tz);
  198.         tzset();
  199.     }
  200. #endif    /* POSIX_SEMANTICS */
  201.  
  202.     for (; *format && s < endp - 1; format++) {
  203.         tbuf[0] = '\0';
  204.         if (*format != '%') {
  205.             *s++ = *format;
  206.             continue;
  207.         }
  208.     again:
  209.         switch (*++format) {
  210.         case '\0':
  211.             *s++ = '%';
  212.             goto out;
  213.  
  214.         case '%':
  215.             *s++ = '%';
  216.             continue;
  217.  
  218.         case 'a':    /* abbreviated weekday name */
  219.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  220.                 strcpy(tbuf, "?");
  221.             else
  222.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  223.             break;
  224.  
  225.         case 'A':    /* full weekday name */
  226.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  227.                 strcpy(tbuf, "?");
  228.             else
  229.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  230.             break;
  231.  
  232. #ifdef SYSV_EXT
  233.         case 'h':    /* abbreviated month name */
  234. #endif
  235.         case 'b':    /* abbreviated month name */
  236.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  237.                 strcpy(tbuf, "?");
  238.             else
  239.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  240.             break;
  241.  
  242.         case 'B':    /* full month name */
  243.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  244.                 strcpy(tbuf, "?");
  245.             else
  246.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  247.             break;
  248.  
  249.         case 'c':    /* appropriate date and time representation */
  250.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  251.                 days_a[range(0, timeptr->tm_wday, 6)],
  252.                 months_a[range(0, timeptr->tm_mon, 11)],
  253.                 range(1, timeptr->tm_mday, 31),
  254.                 range(0, timeptr->tm_hour, 23),
  255.                 range(0, timeptr->tm_min, 59),
  256.                 range(0, timeptr->tm_sec, 61),
  257.                 timeptr->tm_year + 1900);
  258.             break;
  259.  
  260.         case 'd':    /* day of the month, 01 - 31 */
  261.             i = range(1, timeptr->tm_mday, 31);
  262.             sprintf(tbuf, "%02d", i);
  263.             break;
  264.  
  265.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  266.             i = range(0, timeptr->tm_hour, 23);
  267.             sprintf(tbuf, "%02d", i);
  268.             break;
  269.  
  270.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  271.             i = range(0, timeptr->tm_hour, 23);
  272.             if (i == 0)
  273.                 i = 12;
  274.             else if (i > 12)
  275.                 i -= 12;
  276.             sprintf(tbuf, "%02d", i);
  277.             break;
  278.  
  279.         case 'j':    /* day of the year, 001 - 366 */
  280.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  281.             break;
  282.  
  283.         case 'm':    /* month, 01 - 12 */
  284.             i = range(0, timeptr->tm_mon, 11);
  285.             sprintf(tbuf, "%02d", i + 1);
  286.             break;
  287.  
  288.         case 'M':    /* minute, 00 - 59 */
  289.             i = range(0, timeptr->tm_min, 59);
  290.             sprintf(tbuf, "%02d", i);
  291.             break;
  292.  
  293.         case 'p':    /* am or pm based on 12-hour clock */
  294.             i = range(0, timeptr->tm_hour, 23);
  295.             if (i < 12)
  296.                 strcpy(tbuf, ampm[0]);
  297.             else
  298.                 strcpy(tbuf, ampm[1]);
  299.             break;
  300.  
  301.         case 'S':    /* second, 00 - 61 */
  302.             i = range(0, timeptr->tm_sec, 61);
  303.             sprintf(tbuf, "%02d", i);
  304.             break;
  305.  
  306.         case 'U':    /* week of year, Sunday is first day of week */
  307.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  308.             break;
  309.  
  310.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  311.             i = range(0, timeptr->tm_wday, 6);
  312.             sprintf(tbuf, "%d", i);
  313.             break;
  314.  
  315.         case 'W':    /* week of year, Monday is first day of week */
  316.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  317.             break;
  318.  
  319.         case 'x':    /* appropriate date representation */
  320.             sprintf(tbuf, "%s %s %2d %d",
  321.                 days_a[range(0, timeptr->tm_wday, 6)],
  322.                 months_a[range(0, timeptr->tm_mon, 11)],
  323.                 range(1, timeptr->tm_mday, 31),
  324.                 timeptr->tm_year + 1900);
  325.             break;
  326.  
  327.         case 'X':    /* appropriate time representation */
  328.             sprintf(tbuf, "%02d:%02d:%02d",
  329.                 range(0, timeptr->tm_hour, 23),
  330.                 range(0, timeptr->tm_min, 59),
  331.                 range(0, timeptr->tm_sec, 61));
  332.             break;
  333.  
  334.         case 'y':    /* year without a century, 00 - 99 */
  335.             i = timeptr->tm_year % 100;
  336.             sprintf(tbuf, "%d", i);
  337.             break;
  338.  
  339.         case 'Y':    /* year with century */
  340.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  341.             break;
  342.  
  343.         case 'Z':    /* time zone name or abbrevation */
  344.             i = 0;
  345.             if (
  346. #ifndef TZNAME_MISSING
  347.                 daylight &&
  348. #endif
  349.                 timeptr->tm_isdst)
  350.                 i = 1;
  351. #ifdef TZNAME_MISSING
  352.             strcpy(tbuf, timeptr->tm_zone);
  353. #else
  354.             strcpy(tbuf, tzname[i]);
  355. #endif
  356.             break;
  357.  
  358. #ifdef SYSV_EXT
  359.         case 'n':    /* same as \n */
  360.             tbuf[0] = '\n';
  361.             tbuf[1] = '\0';
  362.             break;
  363.  
  364.         case 't':    /* same as \t */
  365.             tbuf[0] = '\t';
  366.             tbuf[1] = '\0';
  367.             break;
  368.  
  369.         case 'D':    /* date as %m/%d/%y */
  370.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  371.             break;
  372.  
  373.         case 'e':    /* day of month, blank padded */
  374.             sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
  375.             break;
  376.  
  377.         case 'r':    /* time as %I:%M:%S %p */
  378.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  379.             break;
  380.  
  381.         case 'R':    /* time as %H:%M */
  382.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  383.             break;
  384.  
  385.         case 'T':    /* time as %H:%M:%S */
  386.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  387.             break;
  388. #endif
  389.  
  390.  
  391. #ifdef VMS_EXT
  392.         case 'v':    /* date as dd-bbb-YYYY */
  393.             sprintf(tbuf, "%2d-%3.3s-%4d",
  394.                 range(1, timeptr->tm_mday, 31),
  395.                 months_a[range(0, timeptr->tm_mon, 11)],
  396.                 timeptr->tm_year + 1900);
  397.             for (i = 3; i < 6; i++)
  398.                 if (islower(tbuf[i]))
  399.                     tbuf[i] = toupper(tbuf[i]);
  400.             break;
  401. #endif
  402.  
  403.  
  404. #ifdef POSIX2_DATE
  405.         case 'C':
  406.             sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
  407.             break;
  408.  
  409.  
  410.         case 'E':
  411.         case 'O':
  412.             /* POSIX locale extensions, ignored for now */
  413.             goto again;
  414.  
  415.         case 'V':    /* week of year according ISO 8601 */
  416. #if defined(GAWK) && defined(VMS_EXT)
  417.         {
  418.             extern int do_lint;
  419.             extern void warning();
  420.             static int warned = 0;
  421.  
  422.             if (! warned && do_lint) {
  423.                 warned = 1;
  424.                 warning(
  425.     "conversion %%V added in P1003.2/11.3; for VMS style date, use %%v");
  426.             }
  427.         }
  428. #endif
  429.             sprintf(tbuf, "%d", iso8601wknum(timeptr));
  430.             break;
  431.  
  432.         case 'u':
  433.         /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
  434.             sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
  435.                     timeptr->tm_wday);
  436.             break;
  437. #endif    /* POSIX2_DATE */
  438.         default:
  439.             tbuf[0] = '%';
  440.             tbuf[1] = *format;
  441.             tbuf[2] = '\0';
  442.             break;
  443.         }
  444.         i = strlen(tbuf);
  445.         if (i)
  446.             if (s + i < endp - 1) {
  447.                 strcpy(s, tbuf);
  448.                 s += i;
  449.             } else
  450.                 return 0;
  451.     }
  452. out:
  453.     if (s < endp && *format == '\0') {
  454.         *s = '\0';
  455.         return (s - start);
  456.     } else
  457.         return 0;
  458. }
  459.  
  460. #ifdef POSIX2_DATE
  461. /* iso8601wknum --- compute week number according to ISO 8601 */
  462.  
  463. #ifndef __STDC__
  464. static int
  465. iso8601wknum(timeptr)
  466. const struct tm *timeptr;
  467. #else
  468. static int
  469. iso8601wknum(const struct tm *timeptr)
  470. #endif
  471. {
  472.     /*
  473.      * From 1003.2 D11.3:
  474.      *    If the week (Monday to Sunday) containing January 1
  475.      *    has four or more days in the new year, then it is week 1;
  476.      *    otherwise it is week 53 of the previous year, and the
  477.      *    next week is week 1.
  478.      *
  479.      * ADR: This means if Jan 1 was Monday through Thursday,
  480.      *    it was week 1, otherwise week 53.
  481.      */
  482.  
  483.     int simple_wknum, jan1day, diff, ret;
  484.  
  485.     /* get week number, Monday as first day of the week */
  486.     simple_wknum = weeknumber(timeptr, 1) + 1;
  487.  
  488.     /*
  489.      * With thanks and tip of the hatlo to ado@elsie.nci.nih.gov
  490.      *
  491.      * What day of the week does January 1 fall on?
  492.      * We know that
  493.      *    (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
  494.      *        (timeptr->tm_wday - jan1.tm_wday) MOD 7
  495.      * and that
  496.      *     jan1.tm_yday == 1
  497.      * and that
  498.      *     timeptr->tm_wday MOD 7 == timeptr->tm_wday
  499.      * from which it follows that. . .
  500.       */
  501.     jan1day = (timeptr->tm_yday - 1) % 7 - timeptr->tm_wday;
  502.     if (jan1day < 0)
  503.         jan1day += 7;
  504.  
  505.     /*
  506.      * If Jan 1 was a Monday through Thursday, it was in
  507.      * week 1.  Otherwise it was last year's week 53, which is
  508.      * this year's week 0.
  509.      */
  510.     if (jan1day >= 1 && jan1day <= 4)
  511.         diff = 0;
  512.     else
  513.         diff = 1;
  514.     ret = simple_wknum - diff;
  515.     if (ret == 0)    /* we're in the first week of the year */
  516.         ret = 53;
  517.     return ret;
  518. }
  519. #endif
  520.  
  521. /* weeknumber --- figure how many weeks into the year */
  522.  
  523. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  524.  
  525. #ifndef __STDC__
  526. static int
  527. weeknumber(timeptr, firstweekday)
  528. const struct tm *timeptr;
  529. int firstweekday;
  530. #else
  531. static int
  532. weeknumber(const struct tm *timeptr, int firstweekday)
  533. #endif
  534. {
  535.     if (firstweekday == 0)
  536.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  537.     else
  538.         return (timeptr->tm_yday + 7 -
  539.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  540. }
  541.  
  542. #if 0
  543. /* ADR --- I'm loathe to mess with ado's code ... */
  544.  
  545. Date:         Wed, 24 Apr 91 20:54:08 MDT
  546. From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
  547. To: arnold@audiofax.com
  548.  
  549. Hi Arnold,
  550. in a process of fixing of strftime() in libraries on Atari ST I grabbed
  551. some pieces of code from your own strftime.  When doing that it came
  552. to mind that your weeknumber() function compiles a little bit nicer
  553. in the following form:
  554. /*
  555.  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
  556.  */
  557. {
  558.     return (timeptr->tm_yday - timeptr->tm_wday +
  559.         (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
  560. }
  561. How nicer it depends on a compiler, of course, but always a tiny bit.
  562.  
  563.    Cheers,
  564.    Michal
  565.    ntomczak@vm.ucs.ualberta.ca
  566. #endif
  567.